<template>
  <div class="col main-content">
    <div class="row tool-bar">
      <div class="col">
        <div class="inline-item">
          <el-input v-model="search.name" clearable style="width: 200px;" placeholder="请输入查询信息"/>
          <el-button @click="searchDatas" icon="el-icon-search" type="primary">查询</el-button>
          <el-button type="primary" @click="createBrand"><i class="fa fa-list"></i> 添加品牌</el-button>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col">
        <el-table
          :data="brands"
          border
          tooltip-effect="dark"
          v-loading="loading"
        >
          <el-table-column
            type="selection"
            width="55"
            fixed align="center">
          </el-table-column>
          <el-table-column
            label="ID" width="80"
          >
            <template slot-scope="scope">
              <div class="center">
                <span>{{scope.row.id}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="名称"
          >
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>


          <el-table-column
            label="创建时间"
            prop="createTime"
            width="160"
          >
          </el-table-column>

          <el-table-column
            label="热门"
            width="50" align="center"
          >

            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.isHot==1"></el-checkbox>
            </template>
          </el-table-column>

          <el-table-column
            label="推荐"
            width="50" align="center"
          >
            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.isRecommend==1"></el-checkbox>
            </template>
          </el-table-column>

          <el-table-column
            label="Logo">
            <template slot-scope="scope">
              <img :src="scope.row.logo" class="brand-logo"/>
            </template>
          </el-table-column>
          <el-table-column
            label="网址">
            <template slot-scope="scope">{{ scope.row.url }}</template>
          </el-table-column>

          <el-table-column
            label="描述">
            <template slot-scope="scope">{{ scope.row.descript }}</template>
          </el-table-column>
          <el-table-column
            label="状态" width="90px" align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.isShow==1" class="el-alert el-alert--success is-center">
                显示
              </div>
              <div v-else class="el-alert el-alert--error is-center">
                已隐藏
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="90px" align="center">
            <template slot-scope="scope">

              <el-button type="text"
                         @click="doEdit(scope.$index, scope.row)">编辑
              </el-button>

              <el-button v-if="scope.row.isShow==0" type="text"
                         @click="changeStatus(scope.$index, scope.row,1)">
                显示
              </el-button>
              <el-button v-else="scope.row.isShow==1" type="text"
                         @click="changeStatus(scope.$index, scope.row,0)">
                隐藏
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="col h-pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import AdminService from 'src/service/admin'
  import TableMix from 'src/components/table-mix'

  export default {
    name: 'AdminBrand',
    mixins: [TableMix],
    data() {
      return {
        brands: [],
        loading: false,
        search: {
          orderBy: 'is_show desc,id',
          name: null,
          pagination: {
            total: 0,
            page: 1,
            limit: 20
          }
        }
      }
    },
    methods: {
      searchDatas: function () {
        this.search.pagination.page = 1
        this.getData()
      },
      handleSizeChange: function (size) {
        this.search.pagination.limit = size
        this.getData()
      },
      handleCurrentChange: function (page) {
        this.search.pagination.page = page
        this.getData()
      },
      //修改状态
      changeStatus: function (index, data, status) {
        const me = this
        const message = status == 1 ? '此操作后将该品牌将不会出现在品牌列表中，是否继续?' : '此操作后将该品牌将出现在品牌列表中，是否继续?'
        me.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          AdminService.brandStatusChange({
            ids: [data.id],
            status: status
          }).then(ds => {
            if (ds != null) {
              data.isShow = status
              me.$message.success('修改状态成功!')
            } else {
              me.$message.error(ds.message)
            }
          })
        }).catch(() => {
        })
      },
      //编辑数据
      doEdit: function (index, data) {
        this.$router.push('/admin/goods/brand/' + data.id + '/edit.html');
      },
      createBrand: function () {
        this.$router.push('/admin/goods/brand/create.html')
      },
      getData: function () {
        this.loading = true
        AdminService.brandSearch(this.search).then(this.processData)
      },
      processData: function (datas) {
        if (datas != null) {
          this.brands = datas.datas
          this.search.pagination = datas.pagination
        }
        this.loading = false
      }
    },
    mounted: function () {
      this.getData(true)
     // this.initTalbeHeight(255)
    }
  }
</script>
